package com.ruantu.clothing.realm;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;

import com.ruantu.clothing.entity.User;
import com.ruantu.clothing.service.UserService;

public class RememberMeFilter extends FormAuthenticationFilter{

    @Autowired
    private UserService userService;

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        Subject subject=getSubject(request,response);
        Session session=subject.getSession();
        if (!subject.isAuthenticated()&&subject.isRemembered()&&session.getAttribute("info")==null){
            Object prinvipal =subject.getPrincipal();
            if (prinvipal!=null){
                String account=prinvipal.toString();
                User user =userService.login(account);
                session.setAttribute("info",user);
            }

        }
        return subject.isAuthenticated()||subject.isRemembered();

    }
}
